from django.contrib import admin
from django.core.cache import cache
from goods.models import Goods, GoodsType, GoodsBanner, IndexPromote, Brand


class BaseModel(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        from celery_tasks.task import static_index

        static_index.delay()
        # 只需要简单的清除点缓存，静态话的首页就可以更新了，但是如果不清楚缓存，视图里面的cache还不
        # 设置过期时间的话，缓存将永远固定不变，所以保险起见还是设置缓存时间为好
        cache.delete('page_cache')

    def delete_model(self, request, obj):
        super().delete_model(request, obj)
        # 为什么要写到这里呢，写到上面还不行
        from celery_tasks.task import static_index
        static_index.delay()
        cache.delete('page_cache')

@admin.register(Goods)
class GoodsAdmin(BaseModel):
    list_display = ['id', 'name', 'brand']


@admin.register(GoodsType)
class GoodsTypeAdmin(BaseModel):
    pass


@admin.register(GoodsBanner)
class GoodsBannerAdmin(BaseModel):
    pass


@admin.register(IndexPromote)
class IndexPromoteAdmin(BaseModel):
    pass


@admin.register(Brand)
class BrandPromoteAdmin(BaseModel):
    pass
